package tasks.leetcode.算法通关;

public class MaxProfit2 {
    public static void main(String[] args) {
        int i = maxProfit(new int[]{1,2,3,4,5});
        int i2 = maxProfit2(new int[]{1,2,3,4,5});
        System.out.println("股票的最大利润:" + i);
        System.out.println("股票的最大利润:" + i2);
    }

    public static int maxProfit2(int[] prices) {
        int maxProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                maxProfit += prices[i] - prices[i - 1];
            }
        }
        return maxProfit;
    }

    private static int maxProfit(int[] prices) {
        if (prices.length <= 1) {
            return 0;
        }
        int maxtProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            int pricei = prices[i];
            int priceImin = prices[i - 1];
            if (pricei > priceImin) {
                maxtProfit += prices[i] - prices[i - 1];
            }
        }
        return maxtProfit;
    }

}
